package net.dgg.zz.credential.talent.service;

import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.StringUtils;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.constant.SystemCommonConstant;
import net.dgg.zz.common.files.entity.Annex;
import net.dgg.zz.common.files.service.AnnexService;
import net.dgg.zz.credential.talent.dao.TalentDao;
import net.dgg.zz.credential.talent.entity.Talent;
import net.dgg.zz.credential.talent.entity.TalentRecord;
import net.dgg.zz.credential.talent.vo.TalentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TalentService {
    @Autowired
    private TalentDao talentDao;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private DesUtil desUtil;
    @Autowired
    private AnnexService annexService;
    @Autowired
    private TalentRecordService talentRecordService;

    public List<Talent> selectTalentByWithPage(Map params){
        try {
            params.put("orderDir", "desc");
            params.put("orderColumn", "create_time");
            List<Talent> list = talentDao.selectTalentByWithPage(params);
            List<Talent> newList = new ArrayList<>(list.size());
            if(list.size()>0){
                for(Talent talent : list){
                    talent.setFiles(this.findFile(talent.getId(),"talent"));
                    newList.add(talent);
                }
            }
            String[] transColum = {"talentSex", "jobStatus", "talentStatus", "education"};
            list = translateUtil.translateList(transColum, newList);//翻译
            String[] hideColum = {"talentPhone"};//解密
            list = desUtil.decryptList(hideColum, list);
            list = CustomerNumberEncryptionUtil.encryptionList(hideColum, list);
            return list;
        }catch (Exception e){
            return null;
        }

    }
    public Integer count(Map params){
        params.put("orderDir", "desc");
        params.put("orderColumn", "create_time");
       return  talentDao.count(params);
    }
    public Integer updateTalent(Map params){
        UserEntity userEntity = sessionManager.getCurrentUser();
        Talent talent = talentDao.selectByPrimaryKey(Long.valueOf(params.get("id").toString()));
        talent.setCertificate(params.get("certificate").toString());
        talent.setCurrentPosition(params.get("currentPosition").toString());
        talent.setCurrentCompany(params.get("currentCompany").toString());
        talent.setEducation(params.get("education").toString());
        talent.setJobArea(params.get("jobArea").toString());
        talent.setJobStatus(params.get("jobStatus").toString());
        talent.setMajor(params.get("major").toString());
        if(!StringUtils.isEmpty(params.get("salaryMax").toString())){
            talent.setSalaryMax( Integer.valueOf(params.get("salaryMax").toString()));
        }
        if(!StringUtils.isEmpty(params.get("salaryMin").toString())){
            talent.setSalaryMin( Integer.valueOf(params.get("salaryMin").toString()));
        }
        talent.setSchool(params.get("school").toString());
        if(!StringUtils.isEmpty(params.get("talentAge").toString())){
            talent.setTalentAge(Integer.valueOf(params.get("talentAge").toString()));
        }
        talent.setTalentName(params.get("major").toString());
        String oldPhone = StringUtils.encryptionNumber(desUtil.decrypt(talent.getTalentPhone()));
        if(!oldPhone.equals(params.get("talentPhone").toString())){
            talent.setTalentPhone(desUtil.encrypt(params.get("talentPhone").toString()));
        }
        talent.setTalentName(params.get("talentName").toString());
        talent.setTalentSex(params.get("talentSex").toString());
        talent.setTargetIndustry(params.get("targetIndustry").toString());
        talent.setUpdaterUser(userEntity);
        //写入操作记录
        TalentRecord talentRecord = new TalentRecord();
        talentRecord.setId(DggKeyWorker.nextId());
        talentRecord.setTalentId(talent.getId());
        talentRecord.setOperateTime(new Date());
        talentRecord.setOperateType("编辑");
        talentRecord.setOperateText("编辑人才");
        talentRecord.setOperateUserId(userEntity.getId());
        talentRecord.setOperateUserName(userEntity.getRealName().concat("/").concat(userEntity.getLoginName()));
        talentRecordService.insert(talentRecord);
        return talentDao.updateByPrimaryKey(talent);
    }
    @Transactional
    public Integer insert(Map params){
        UserEntity userEntity = sessionManager.getCurrentUser();
        Talent talent = new Talent();
        talent.setId(Long.valueOf(params.get("id").toString()));
        talent.setCertificate(params.get("certificate").toString());
        talent.setCurrentPosition(params.get("currentPosition").toString());
        talent.setEducation(params.get("education").toString());
        talent.setJobArea(params.get("jobArea").toString());
        talent.setJobStatus(params.get("jobStatus").toString());
        talent.setCurrentCompany(params.get("currentCompany").toString());
        talent.setMajor(params.get("major").toString());
        if(!StringUtils.isEmpty(params.get("salaryMax").toString())){
            talent.setSalaryMax( Integer.valueOf(params.get("salaryMax").toString()));
        }
        if(!StringUtils.isEmpty(params.get("salaryMin").toString())){
            talent.setSalaryMin( Integer.valueOf(params.get("salaryMin").toString()));
        }
        talent.setSchool(params.get("school").toString());
        if(!StringUtils.isEmpty(params.get("talentAge").toString())){
            talent.setTalentAge(Integer.valueOf(params.get("talentAge").toString()));
        }
        talent.setTalentName(params.get("talentName").toString());
        talent.setTalentPhone(desUtil.encrypt(params.get("talentPhone").toString()));
        talent.setTalentSex(params.get("talentSex").toString());
        talent.setTargetIndustry(params.get("targetIndustry").toString());
        talent.setTalentStatus(CredentialConstant.TATLENT_STATUS_UNMATCH);
        talent.setCreateUser(userEntity);
        //写入操作记录
        TalentRecord talentRecord = new TalentRecord();
        talentRecord.setId(DggKeyWorker.nextId());
        talentRecord.setTalentId(Long.valueOf(params.get("id").toString()));
        talentRecord.setOperateTime(new Date());
        talentRecord.setOperateType("新增");
        talentRecord.setOperateText("新增人才");
        talentRecord.setOperateUserId(userEntity.getId());
        talentRecord.setOperateUserName(userEntity.getRealName().concat("/").concat(userEntity.getLoginName()));
        talentRecordService.insert(talentRecord);
        return talentDao.insert(talent);
    }

    /**
     * 通过id电话号码
     * @param id
     * @return
     */
    public String getTalentPhoneById(Long id){
        String returnStr = null;
         Talent talent = talentDao.selectByPrimaryKey(id);
         if(null != talent){
             returnStr = desUtil.decrypt(talent.getTalentPhone());
         }
        return returnStr;
    }

    /**
     * 查找附件
     * @param id
     * @param type
     * @return
     */
    public String findFile(Long id,String type){
        String fileId = SystemCommonConstant.ANNEX_FILE_ID_PREFIX + "_" + id+ "_" + type;
        List<Annex> files = annexService.findFile(fileId);
        String returnStr="";
        if (null != files) {
            for (Annex file : files) {
                returnStr +=file.getFilename()+"<br/>";
            }
        }
        return returnStr;
    }

    /**
     * 通过姓名，电话判重
     * @param name
     * @param phone
     * @return
     */
    public boolean isExist(String name,String phone){
        Map params = new HashMap();
        params.put("name",name);
        params.put("phone",desUtil.encrypt(phone));
        Integer count = talentDao.queryExist(params);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }

    public Talent getTalenById(Long id) {
        Talent talent = talentDao.selectByPrimaryKey(id);
        talent.setTalentPhone(StringUtils.encryptionNumber(desUtil.decrypt(talent.getTalentPhone())));
        return talent;
    }

    public Integer updateStatus(Map params) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        Talent talent = talentDao.selectByPrimaryKey(Long.valueOf(params.get("id").toString()));
        if(params.containsKey("matchCompanyId") && !StringUtils.isEmpty(params.get("matchCompanyId").toString())){
            talent.setMatchCompanyId(Long.valueOf(params.get("matchCompanyId").toString()));
        }
        if(null == talent.getMatchUserName()) {
            if (CredentialConstant.TATLENT_STATUS_MATCH.equals(params.get("talentStatus").toString()) || CredentialConstant.TATLENT_STATUS_ENTRY.equals(params.get("talentStatus").toString())) {
                talent.setMatchUserId(userEntity.getId());
                talent.setMatchUserName(userEntity.getRealName().concat(userEntity.getLoginName()));
                talent.setMatchUserOrgId(userEntity.getOrgId());
            }
        }
        if(CredentialConstant.TATLENT_STATUS_UNMATCH.equals(params.get("talentStatus").toString())){
            talent.setMatchUserId(null);
            talent.setMatchUserName(null);
            talent.setMatchUserOrgId(null);
        }
        talent.setTalentStatus(params.get("talentStatus").toString());
        //写入操作记录
        TalentRecord talentRecord = new TalentRecord();
        talentRecord.setId(DggKeyWorker.nextId());
        talentRecord.setTalentId(talent.getId());
        talentRecord.setOperateTime(new Date());
        talentRecord.setOperateType("状态更新");
        talentRecord.setOperateText("状态更新"+"/"+params.get("talentStausName").toString());
        talentRecord.setOperateUserId(userEntity.getId());
        talentRecord.setOperateUserName(userEntity.getRealName().concat("/").concat(userEntity.getLoginName()));
        talentRecordService.insert(talentRecord);
        return talentDao.updateByPrimaryKey(talent);
    }

    public String getEntyTalent(Long companyId){
        return talentDao.queryEntry(companyId);
    }

    public List<TalentVo> findExportTalent(Map params) {
        List<Talent> list  = this.selectTalentByWithPage(params);
        List<TalentVo> newList = new LinkedList<> ();
        for(Talent talent : list){
            TalentVo talentVo = new TalentVo();
            talentVo.setCertificate(null == talent.getCertificate() ? "" : talent.getCertificate());
            talentVo.setCurrentCompany(null == talent.getCurrentCompany() ? "" :talent.getCurrentCompany());
            talentVo.setCurrentPosition(null == talent.getCurrentPosition() ? "" : talent.getCurrentPosition());
            talentVo.setEducation(null == talent.getEducation() ? "" : talent.getEducation());
            talentVo.setJobArea(null == talent.getJobArea() ? "" : talent.getJobArea());
            talentVo.setMajor(null == talent.getMajor() ? "" : talent.getMajor());
            talentVo.setJobStatus(null == talent.getJobStatus() ? "" :talent.getJobStatus());
            talentVo.setMatchUserName(null == talent.getMatchUserName() ? "" : talent.getMatchUserName());
            talentVo.setSchool(null == talent.getSchool() ? "" :talent.getSchool());
            talentVo.setTalentAge(null == talent.getTalentAge() ? "" : talent.getTalentAge().toString());
            talentVo.setTalentName(null == talent.getTalentName() ? "" : talent.getTalentName());
            talentVo.setTalentPhone(null == talent.getTalentPhone() ? "" :talent.getTalentPhone());
            talentVo.setTalentSex(null == talent.getTalentSex() ? "" :talent.getTalentSex());
            talentVo.setTalentStatus(null == talent.getTalentStatus() ? "":talent.getTalentStatus());
            talentVo.setTargetIndustry(null == talent.getTargetIndustry() ? "" : talent.getTargetIndustry());
            String salary ="";
            if(null != talent.getSalaryMin() && null != talent.getSalaryMax()){
                salary = talent.getSalaryMin().toString()+"—"+talent.getSalaryMax().toString()+"K";
            }
            talentVo.setSalary(salary);
            newList.add(talentVo);
        }
        return newList;
    }
    public boolean isExistEdit(Map params){
        params.put("talentPhone",desUtil.encrypt(params.get("talentPhone").toString()));
        Integer count = talentDao.queryExistEdit(params);
        if(count>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取未解码的人才
     * @param id
     * @return
     */
    public Talent getTalenByIdNotDe(Long id) {
        Talent talent = talentDao.selectByPrimaryKey(id);
        return talent;
    }

    public List<TalentRecord> getRecordList(Map params){
        return talentRecordService.selectWithPage(params);
    }

    public Integer saveRecord(Map params) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        TalentRecord talentRecord = new TalentRecord();
        talentRecord.setId(DggKeyWorker.nextId());
        talentRecord.setTalentId(Long.valueOf(params.get("talentId").toString()));
        talentRecord.setOperateTime(new Date());
        talentRecord.setOperateType("备注");
        talentRecord.setOperateText(params.get("text").toString());
        talentRecord.setOperateUserId(userEntity.getId());
        talentRecord.setOperateUserName(userEntity.getRealName().concat("/").concat(userEntity.getLoginName()));
        return talentRecordService.insert(talentRecord);
    }
}
